# coding:utf-8

import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
import json

from tornado.options import options
from tornado.web import RequestHandler

tornado.options.define("port", default=8000, type=int, help="run server on the given port")


class IndexHandler(RequestHandler):
    """请求处理类"""
    def get(self):
        self.write("hello")
        # self.send_error(404, title="error_title", content="itcast error")
        error_data = {
            "title": "error_title",
            "content": "itcast error"
        }
        self.send_error(404, **error_data)
        # self.send_error(404, title="error_title", content="itcast error")

        # self.write("after")

    def write_error(self, status_code, **kwargs):
        self.write(u"<h1>出错了，程序员GG正在赶过来！</h1>")
        self.write(u"<p>错误名：%s</p>" % kwargs["title"])
        self.write(u"<p>错误详情：%s</p>" % kwargs["content"])


if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        [(r"/", IndexHandler)],
        debug=True
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()